package com.sunfeng.chap5.selectkey;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * 相同的通道可以注册到不同的选择器，返回的 Selection Key 不是同一个对象
 */
public class Test9 {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel1 = ServerSocketChannel.open();
        serverSocketChannel1.bind(new InetSocketAddress("localhost", 8888));
        serverSocketChannel1.configureBlocking(false);
        Selector selector1 = Selector.open();
        Selector selector2 = Selector.open();
        SelectionKey selectionKey1 = serverSocketChannel1.register(selector1, SelectionKey.OP_ACCEPT);
        System.out.println(selectionKey1.hashCode());
        SelectionKey selectionKey2 = serverSocketChannel1.register(selector2, SelectionKey.OP_ACCEPT);
        System.out.println(selectionKey2.hashCode());
        serverSocketChannel1.close();
    }
}
